package View.JPanel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import Management.RuleList;
import View.CoolSwing.CurvesPanel;
import View.CoolSwing.GradientPanel;
import View.CoolSwing.StackLayout;
import View.JTable.JAnimationTable;
import View.JTree.JAnimationTree;
import View.JTree.JAnimationTreeNode;
import View.JTree.MyTreeCellRenderer;

public class JAnimatorPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1748632743079023596L;
	private GradientPanel 	gradient;
	private CurvesPanel		curves;
	private JPanel			contentPanel;	
	private	JAnimationTree	jAnimationTree;
	private JAnimationTable	jAnimationTable;
//	private FileViewPanelTest jAnimationTable;
	private RuleList		ruleList;
	private JScrollPane 	jsp1;
	private JScrollPane 	jsp2;
	private JPanel			jpRight;
	private JPanel 			jpLeft;
	private JInformationPanel jInfo;
	private JArrorwPanel 	jap;
	
	/**
	 * 构造方法，
	 * 利用Stacklayout来建立3个JPanel，
	 * 主要的内容窗格是ContentJPanel
	 */
	public JAnimatorPanel(int index) {
		initPanel(index);
        startAnimation();
	}
	
	/**
	 * init
	 */
	private void initPanel(int index) {
        setLayout(new StackLayout());
        gradient 	 = new GradientPanel();
        contentPanel = new ContentPanel(index);
        curves 		 = new CurvesPanel();
        
        add(gradient, StackLayout.TOP);
        add(contentPanel, StackLayout.TOP);
//        add(curves, StackLayout.TOP);
	}
	
	/**
	 * 开始动画
	 * @author zmm
	 */
    private void startAnimation() {
        Timer timer = new Timer(50, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                curves.animate();
                curves.repaint();
            }
        });
        timer.start();
    }
    
    /**
     * 包含主要内容的JPanel
     * @author zmm
     *
     */
    protected class ContentPanel extends JPanel implements TreeSelectionListener, MouseListener {
        /**
		 * 
		 */
		private static final long serialVersionUID = 5991565110610862647L;
		private JPopupMenu popup;
		
		/**
         * Construction of hte ContentPanel;
         * @param index
         */
    	public ContentPanel(int index) {
        	//使ContentPanel透明
        	setOpaque(false);
        	
        	//layout
        	setLayout(new BorderLayout());
        	
        	//JAnimationTree使用
        	ruleList 	   = new RuleList();
    		//mainPath
    		String mainPath = ruleList.getList().get(index).getMainPath();
        	jAnimationTree = new JAnimationTree(mainPath);
        	jAnimationTree.setCellRenderer(new MyTreeCellRenderer());
        	jAnimationTree.addTreeSelectionListener(this);
        	jsp1 		   = new JScrollPane(jAnimationTree);
        	jsp1.setPreferredSize(new Dimension(200,150));
        	jAnimationTable= new JAnimationTable(index, new File(mainPath));	
//        	jAnimationTable= new FileViewPanelTest(new File(mainPath), index);
        	jAnimationTable.getHistory().add(new File(mainPath));
        	jAnimationTable.setNow(jAnimationTable.getNow() + 1);
			
        	jsp2 = new JScrollPane(jAnimationTable);
        	jsp2.addMouseListener(this);
        	popup = jAnimationTable.getJPopupMenu();
        	        	
        	//使jsp1,jsp2背景透明
        	jsp1.setOpaque(false);
        	jsp1.getViewport().setOpaque(false);   
        	jsp1.setBorder(null);
        	jsp2.setOpaque(false);
        	jsp2.getViewport().setOpaque(false);
//        	jsp2.setBorder(null);

        	//JPRight with jsp2 and JInformationPanel and jArrorwPanel
        	jap = new JArrorwPanel();
        	jpRight = new JPanel();
        	jpRight.setOpaque(false);
        	jpRight.setLayout(new BorderLayout());
        	jpRight.add("Center",jsp2);
        	jInfo = new JInformationPanel(new File(mainPath));
        	jInfo.setOpaque(false);
        	//regist the jInfo to table
        	jAnimationTable.registJInfo(jInfo);
        	jpRight.add("South", jInfo);
        	//regist the jap to table
        	jAnimationTable.registJArrorw(jap);
        	jap.registJAnime(jAnimationTable);
//        	jap.registFileViewPanelTest(jAnimationTable);
        	jap.setAddress(mainPath);
        	jpRight.add("North",jap);
        	
        	//JPLeft with jsp1 and jLabel
        	jpLeft = new JPanel();
        	jpLeft.setOpaque(false);
        	jpLeft.setLayout(new BorderLayout());
        	jpLeft.add("Center",jsp1);
        	jpLeft.add("East",new JSeparator(SwingConstants.VERTICAL));
        	
        	add("West",jpLeft);
        	add("Center",jpRight);
        }
        
    	/**
    	 * Do the things when the Tree's value changed;
    	 */
    	@Override
    	public void valueChanged(TreeSelectionEvent e) {
    		if (e.getSource() == jAnimationTree) {
    			JAnimationTreeNode jtn = (JAnimationTreeNode) e.getPath().getLastPathComponent();
    			jAnimationTable.setDirectionary(jtn.getFile());
//    			jAnimationTable.setDirectory(jtn.getFile());
    			
    			for (int i = jAnimationTable.getNow() + 1; i < jAnimationTable.getHistory().toArray().length; i++) {
    				jAnimationTable.getHistory().remove(jAnimationTable.getNow() + 1);
    			}
    			jAnimationTable.getHistory().add(jtn.getFile());
    			jAnimationTable.setNow(jAnimationTable.getNow() + 1);
    		}
    	}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
    }
}
